<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/09 14:39
 * Blog：www.myblogs.xyz
 */

namespace app\common\model;


class PlatformWallet extends Common
{
    protected $name = 'platform_wallet';
    protected $pk = 'wallet_id';

    public function walletInc($money = 0, $value = '')
    {
        return $this->setWallet($money, $value, 1);
    }

    public function walletDnc($money = 0, $value = '')
    {
        return $this->setWallet($money, $value, 2);
    }

    protected function setWallet($money, $value, $type = 1)
    {
        if (empty($money) || empty($value)) return false;
        try{
            $result = $this->lock(true)->where($this->pk, 1)->value($value);
            if ($type == 1){
                $new_result = $result + floatval(round($money, 2));
            }else{
                $new_result = $result - floatval(round($money, 2));
            }
            $this->where($this->pk, 1)->update([$value=>$new_result]);
            // 提交事务
            $this->DbCommit();
        } catch (\Exception $e) {
            // 回滚事务
            $this->Dbrollback();
            return false;
        }
        return true;
    }

}